package com.neusoft.elmboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.BusinessService;


@RestController
@RequestMapping("/Business")
public class BusinessController {
	@Autowired
	private BusinessService businessService;
	
	@RequestMapping(value = "/orderTypeId/{orderTypeId}", method = RequestMethod.GET)
	public List<Business> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId) throws Exception{
		return businessService.listBusinessByOrderTypeId(orderTypeId);
	}

	@RequestMapping(value = "/businessId/{businessId}/password/{password}" ,method = RequestMethod.GET)
	public Business getBusinessByIdByPass(
		@PathVariable("businessId") Integer businessId,
        @PathVariable("password") String password) throws Exception{
		return businessService.getBusinessByIdByPass(businessId,password);
	}

	
	
	@RequestMapping(value = "/businessId/{businessId}", method = RequestMethod.GET)
	public Business getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception{
		return businessService.getBusinessById(businessId);
	}
	

	@RequestMapping(method = RequestMethod.POST)
	public int saveBusiness(Business business){
		return businessService.saveBusiness(business);
	}

	@RequestMapping(method = RequestMethod.PUT)
	public int updateBusiness(Business business) {
		return businessService.updateBusiness(business);
	}
	@RequestMapping(value = "/businessId/oldPass/newPass", method = RequestMethod.PUT)
    public int updateBusinessPassword(Integer businessId,String oldPass,String newPass) throws Exception{
        return businessService.updateBusinessPassword(businessId, oldPass, newPass);
    }
	 // 新增方法：随机获取指定数量的商家
    @RequestMapping(value = "/orderTypeId/{orderTypeId}/limit/{limit}", method = RequestMethod.GET)
    public List<Business> listRandomBusinesses(
        @PathVariable("orderTypeId") Integer orderTypeId,
        @PathVariable("limit") Integer limit) throws Exception {
        return businessService.listRandomBusinessesByOrderTypeId(orderTypeId, limit);
    }
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public List<Business> searchBusiness(@RequestParam("name") String name) {
		return businessService.searchBusinessByName(name);
	}
}